<?php
/**
 * 报名管理类
 * @author Administrator
 *
 */
class EnrollAction extends BaseAction{
	/**
	 * 展示报名列表方法
	 */
	public function enroll_list(){
		$where['state'] = 1;
		$where = $this->my_where($where);
		import('ORG.Util.Page');// 导入分页类
		$count = M('Enroll')->where($where)->count();
		$Page = new Page($count,10);//每页显示10条
		$show = $Page->show();
		$a_list = M('Enrollment_area')->select();
		$a_list_ = array();
		//获取招生地区
		foreach ($a_list as $k=>$v){
			$a_list_[$v['id']] = $v;
		}
		$list = M('Enroll')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		//通过user_id获取用户名
		foreach ($list as $k=>$v){
			$list[$k]['user'] = M('User')->where('id = '.$v['user_id'])->find();
		}
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->assign("a_list_",$a_list_);
		$this->assign("a_list",$a_list);
		$this->display();
	}
	public function eroll_detail(){
		$obj = M('Enroll')->find($_GET['id']);
		//查询招生地区
		//获取学生奖项 返回数组
		$prize_arr = explode(',', $obj['prize']);
		$ach_arr = explode(',', $obj['achievement']);
		$this->assign('obj',$obj);
		$this->assign('prize_arr',$prize_arr);
		$this->assign('ach_arr',$ach_arr);
		$this->display();
	}
	public function img_out(){
		$obj=M('Enroll')->find($_GET['id']);
		$img = file_get_contents(C('BM_IMG_PATH').'/'.$obj['stub_pic'],true);
		ob_end_clean();
		//使用图片头输出浏览器
		header("Content-Type: image/jpeg;text/html; charset=utf-8");
		echo $img;
		exit;
	}	
	//报名开启设置
	public function enroll_set(){
		if($_POST){
			M('Cost')->where('id=2')->save($_POST);
			$this->success('设置成功');
			exit;
		}
		$obj = M('Cost')->where('id=2')->find();
		$this->assign('obj',$obj);
		$this->display();
	}
	//导出excel
	public function excel_out(){
		vendor("PHPExcel.PHPExcel");
		$objPHPExcel = new \PHPExcel();
		$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);//设置保存版本格式
		$list = M('Enroll')->select();
		ob_clean();
		//设置单元格的值
		$objPHPExcel->getActiveSheet()->setCellValue('A1', '序号');
		$objPHPExcel->getActiveSheet()->setCellValue('B1', '学生姓名');
		$objPHPExcel->getActiveSheet()->setCellValue('C1', '民族');
		$objPHPExcel->getActiveSheet()->setCellValue('D1', '报名号');
		$objPHPExcel->getActiveSheet()->setCellValue('F1', '性别');
		$objPHPExcel->getActiveSheet()->setCellValue('E1', '招生地区');
		$objPHPExcel->getActiveSheet()->setCellValue('G1', '年制');
		$objPHPExcel->getActiveSheet()->setCellValue('H1', '报名时间');
		$objPHPExcel->getActiveSheet()->setCellValue('I1', '是否缴费');
		foreach ($list as $key => $value) {
			$i=$key+2;//表格是从1开始的
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$i,  $key+1);//这里是设置A2单元格的内容
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$i,  $value['name']);////这里是设置B2单元格的内容
			if($value['nation']==1){
				$nation = '汉族';
			}elseif($value['nation']==2){
				$nation = '少数民族';
			}
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$i,  $nation);////这里是设置C2单元格的内容
			$objPHPExcel->getActiveSheet()->setCellValue('D'.$i,  $value['enroll_num']);////这里是设置C2单元格的内容
			if($value['sex']==1){
				$val = '男';
			}elseif($value['sex']==0){
				$val = '女';
			}
			$objPHPExcel->getActiveSheet()->setCellValue('E'.$i,$val);////这里是设置D2单元格的内容
			$objPHPExcel->getActiveSheet()->setCellValue('F'.$i,$value['s_province'].$value['s_city'].$value['s_county']);////这里是设置E2单元格的内容
			if($value['year']==1){
				$year = '三年';
			}elseif($value['year']==2){
				$year = '四年';
			}
			$time = date('Y-m-d',$value['create_time']);
			$objPHPExcel->getActiveSheet()->setCellValue('G'.$i,$year);////这里是设置F2单元格的内容
			$objPHPExcel->getActiveSheet()->setCellValue('H'.$i,$time);////这里是设置G2单元格的内容IF
			if($value['state']==1){
				$state = '是';
				
			}elseif($value['state']==0){
				$state = '否';
			}			
			$objPHPExcel->getActiveSheet()->setCellValue('I'.$i,$state);////这里是设置H2单元格的内容
			//以此类推，可以设置C D E F G看你需要了。
		
		}
		Header('content-Type:application/vnd.ms-excel;charset=utf-8');
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
		header("Content-Type:application/force-download");
		header("Content-Type:application/vnd.ms-execl");
		header("Content-Type:application/octet-stream");
		header("Content-Type:application/download");;
		header('Content-Disposition:attachment;filename=报名信息统计表.xls');
		//header("Content-Disposition:attachment;filename=export_productmap.xls");
		header("Content-Transfer-Encoding:binary");
		$objWriter->save('报名信息统计表.xls');
		$objWriter->save('php://output');
	}		
	//搜索配置
	public function my_where($where){
		$search_word = $_GET['search_word'];
		if($search_word) {
			$where['name|s_county|s_city|s_province|enroll_num'] = array('like',"%$search_word%");
		}
		
		if($_GET['start_time'] && $_GET['end_time']){
			$where['create_time'] = array('between',array(strtotime($_GET['start_time'].' 00:00:00'),strtotime($_GET['end_time'].' 23:59:59')));
		}elseif ($_GET['start_time']){
			$where['create_time'] = array('GT',strtotime($_GET['start_time'].' 00:00:00'));
		}elseif ($_GET['end_time']){
			$where['create_time'] = array('LT',strtotime($_GET['end_time'].' 23:59:59'));
		}
		
		if($_GET['s_county']){
			$where['s_county'] = $_GET['s_county'];
		}elseif ($_GET['s_city']){
			$where['s_city'] = $_GET['s_city'];
		}elseif ($_GET['s_province']){
			$where['s_province'] = $_GET['s_province'];
		}
		return $where;
	}
	
	
	
	
	
	
	//==============以下是杨剑扩展代码
	
	
	public function get_area(){
		$name = $_POST['name'];
		$leave = $_POST['leave'];
		$result = '';
		if($leave == 0){//获取省份
			$result = M('Enroll')->distinct (true)->where ('state=1')->field (array('s_province'=>'name'))->select();
		}elseif ($leave == 1){//获取城市
			$result = M('Enroll')->distinct (true)->where ("state=1 and s_province='$name'")->field (array('s_city'=>'name'))->select();
		}elseif ($leave == 2){//获取区域
			$result = M('Enroll')->distinct (true)->where ("state=1 and s_city='$name'")->field (array('s_county'=>'name'))->select();
		}
		$this->ajaxReturn($result);
	}
	
	public function excel(){
		$where['state'] = 1;
		$where = $this->my_where($where);
		$list = M('Enroll')->where($where)->order('id desc')->select();
		
		vendor("PHPExcel.PHPExcel");
			$objPHPExcel = new PHPExcel();
			$objPHPExcel->getProperties()->setCreator("ctos")
			->setLastModifiedBy("ctos")
			->setTitle("Office 2007 XLSX Test Document")
			->setSubject("Office 2007 XLSX Test Document")
			->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
			->setKeywords("office 2007 openxml php")
			->setCategory("Test result file");
		
		
		$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
			$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
			$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
			$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18);
			$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
			$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
			$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
			$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(18);
			$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(18);
			$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(18);
			$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
			$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(30);
			$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(30);
			$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(15);
			$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(15);
			$objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(15);
			$objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(15);
			
			
			$objPHPExcel->setActiveSheetIndex(0)
			->setCellValue('A1', '序号')
			->setCellValue('B1', '姓名')
			->setCellValue('C1', '性别')
			->setCellValue('D1', '民族')
			->setCellValue('E1', '身份证')
			->setCellValue('F1', '报名号')
			->setCellValue('G1', '年制')
			->setCellValue('H1', '招生地区')
			->setCellValue('I1', '户籍')
			->setCellValue('J1', '是否教师子女')
			->setCellValue('K1', '家庭地址')
			->setCellValue('L1', '父亲电话')
			->setCellValue('M1', '母亲电话')
			->setCellValue('N1', '父母姓名')
			->setCellValue('O1', '工作单位')
			->setCellValue('P1', '缴费信息(元)')
			->setCellValue('Q1', '报名时间');
			
			if($list)
			foreach ($list as $k=>$v){
				$create_time = $v['create_time']?date("Y-m-d H:i:s",$v['create_time']):'';
				$objPHPExcel->getActiveSheet(0)->setCellValue('A'.($k+2), $k+1);
				$objPHPExcel->getActiveSheet(0)->setCellValue('B'.($k+2), $v['name']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('C'.($k+2), $v['sex']?'男':'女');
				$objPHPExcel->getActiveSheet(0)->setCellValue('D'.($k+2), $v['nation']==1?'汉族':'少数民族');
				$objPHPExcel->getActiveSheet(0)->setCellValue('E'.($k+2), "'".$v['id_card']."'");
				$objPHPExcel->getActiveSheet(0)->setCellValue('F'.($k+2), $v['enroll_num']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('G'.($k+2), $v['year']==1?'三年':'四年');
				$objPHPExcel->getActiveSheet(0)->setCellValue('H'.($k+2), $v['s_province'].$v['s_city'].$v['s_county']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('I'.($k+2), $v['household_register']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('J'.($k+2), $v['teacher_child']==1?'是':'否');
				$objPHPExcel->getActiveSheet(0)->setCellValue('K'.($k+2), $v['address']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('L'.($k+2), $v['father_phone']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('M'.($k+2), $v['mother_phone']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('N'.($k+2), $v['parent_name']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('O'.($k+2), $v['work_place']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('P'.($k+2), $v['cost']);
				$objPHPExcel->getActiveSheet(0)->setCellValue('Q'.($k+2), $create_time);
			}
		
		
			$objPHPExcel->getActiveSheet()->setTitle('报名信息汇总表');
			$objPHPExcel->setActiveSheetIndex(0);
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="报名信息汇总表('.date('Y-m-d H:i:s').').xls"');  //日期为文件名后缀
			header('Cache-Control: max-age=0');
			$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel5为xls格式，excel2007为xlsx格式
			$objWriter->save('php://output');
	}
	
	
	
	
	
	
}

?>